from conf.Edit.MyLael import *
from conf.Edit.EditEvents import *
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   MapMenus
@Time    :   2023/02/24 21:05:22
@author  :   Eval
@Version :   1.0
@Describe:   编辑器的菜单封装
'''

class MapMenus:
    def __init__(self,parent) -> None:
        self.form:MyWidget = parent
        # 当前选中的坐标点
        self.CurrowPoint:list = None
        # 当前选中的地板
        self.CurrowLab:MyLabel = None
        
        # 菜单主窗口
        self.menuWin:QLabel = self.form.ui.createLab(None,w=80)
        self.menuWin.setStyleSheet("border:1px solid orange;background-color:white")

        # 显示当前选中的坐标点
        self.CurrowLabText:QLabel = self.form.ui.createLab(None,w=self.menuWin.width()-20)
        # 清除当前选中点按钮
        self.BtnClearPoint:QPushButton = self.form.ui.createBtnBox("清除元素",event=self.rmMapPoint)
        # 编辑事件
        self.BtnEditEvent:QPushButton = self.form.ui.createBtnBox("编辑事件",event=self.editEvents)
        # 清除当前选中点的事件
        self.BtnClearEvent:QPushButton = self.form.ui.createBtnBox("清除事件",event=self.rmMapEvent)
        self.BtnClearEvent.setStyleSheet("background-color:rgb(225,92,92);color:rgb(255,255,255);")
        # 隐藏菜单
        self.BtnHideMenus:QPushButton = self.form.ui.createBtnBox("隐藏菜单",event=self.hide)
        # 设置出生点
        self.BtnSetHeroPos:QPushButton = self.form.ui.createBtnBox("设为出生点",event=self.SetHerPos,MW=70)
        # 停止当前素材的动画播放,除了 门所有的动画帧大于 1的都播放
        # self.BtnStopAnim:QPushButton = self.form.ui.createBtnBox("切换播放",event=self.changeAnimStatr)
        # 加入列表里, 方便统一管理
        self.controls:list[QPushButton] = [self.CurrowLabText,self.BtnClearPoint,self.BtnClearEvent,self.BtnEditEvent,self.BtnHideMenus,self.BtnSetHeroPos]
        self.menuWin.setFixedHeight(len(self.controls)*30)

        self.hide()

    def show(self,pos=None,lab=None,times32=32,offstX=15,offstY=15):
        """传入的坐标值根据形参:times32 判断是否需要乘以32"""
        x,y = 0,0
        # 倍数只能是1 或者32
        if times32 != 32:
            times32 = 1
        if not pos is None:
            if type(pos) == str:
                Spos = pos.split(",")
                x,y = int(Spos[1])*times32 +offstX,int(Spos[0])*times32 +offstY
            elif type(pos) == list:
                x,y = int(pos[1])*times32 +offstX,int(pos[0])*times32 +offstY
            else:
                self.form.addLog("无效坐标",level=3)
                return

        self.move(x,y)
        self.CurrowPoint = [y//32,x//32]
        self.CurrowLab = lab
        self.CurrowLabText.setText("".join(str(self.CurrowPoint)))
        """显示菜单 并根据参数移动位置"""
        self.menuWin.setVisible(True)
        self.menuWin.raise_() # 修改层级,保证菜单的层级优先
        for i in self.controls:
            i.setVisible(True)
            i.raise_()

    def hide(self):
        """隐藏菜单"""
        self.menuWin.setVisible(False)
        for i in self.controls:
            i.setVisible(False)

    def move(self,x:int,y:int):
        """更新菜单的坐标"""
        self.menuWin.move(x,y)
        offst = 10
        offstX = 0
        for i in self.controls:
            # 将按钮设置为相对主菜单弹窗的居中位置
            offstX = (int(self.menuWin.width()) - int(i.width())) // 2
            if offstX < 0:
                offstX = 0
            i.move(self.menuWin.x()+offstX,self.menuWin.y()+offst)
            offst += 25

    def reSize(self,w:int,h:int):
        """修改菜单尺寸"""
        self.menuWin.setFixedWidth(w)
        self.menuWin.setFixedHeight(h)

    def rmMapPoint(self):
        """清除当前选中的地图点"""
        self.CurrowLab.updateLab(self.CurrowPoint[0],self.CurrowPoint[1])
        self.hide()

    def changeAnimStatr(self):
        """
        (弃用的API)
        切换当前地图点的动画播放状态
        """
        self.CurrowLab.playAnim = not self.CurrowLab.playAnim
        # 如果是停止, 那么将当前坐标点的素材重置
        if not self.CurrowLab.playAnim:
            self.CurrowLab.setPixmap(self.CurrowLab.pix)
            self.CurrowLab.animIndex = 0

    def editEvents(self):
        self.hide()
        if self.form.EventManage is None:
            self.form.EventManage = ChildUI(self.form)
            self.form.EventManage.showWin(pos=self.CurrowPoint)
        else:
            # 如果事件编辑器已经被实例化过了,  那么再次调用时间编辑器的时候 直接show显示就好了
            self.form.EventManage.showWin(pos=self.CurrowPoint)

    def rmMapEvent(self):
        """清空指定点的事件"""
        if self.form.mapEvn.get(f"{self.CurrowPoint[0]},{self.CurrowPoint[1]}"):
            if self.form.ui.ShowDiaLog_Window("确认清除此点的事件?此操作不可逆") == DiaLogResult.OK:
                del self.form.mapEvn[f"{self.CurrowPoint[0]},{self.CurrowPoint[1]}"]
                self.form.IsSaveMap = False
                self.form.saveMap()
                self.form.updateMap()
                self.form.addLog(f"清除事件点完成")
                # 关闭菜单
                self.hide()

    def SetHerPos(self):
        """设置主角出生点"""
        self.form.GC.GC_Scripts["Hero"]["x"] = self.CurrowPoint[0]
        self.form.GC.GC_Scripts["Hero"]["y"] = self.CurrowPoint[1]
        self.form.GC.GC_Scripts["MainFloor"] = self.form.FloorName
        with open(f"conf/GlobalScript.json","w",encoding="utf8") as file:
            file.write(JsonSave(self.form.GC.GC_Scripts))
        self.hide()
        self.form.addLog(f"重置出生点完成",level=1)
        # 移动代表主角的图标到这个点
        
        self.form.renderHeroPos.move(int(self.CurrowPoint[1])*32,int(self.CurrowPoint[0])*32)
        self.form.renderHeroPos.setVisible(True)
